﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Lapas.Common.Forms;
using WorkDAL;
using LookupControl;
using WorkDAL.Meterials;
using Lapas.Common;

namespace Material
{
    public partial class MaterialGroupEditForm : EditForm
    {

        public LiMaterialGroup liMaterialGroup = new LiMaterialGroup();

        public MaterialGroupEditForm()
        {
            InitializeComponent();
            base.OkButton = btnOK;
            base.CloseButton = btnClose;
        }

        private void chkGroupEnd_CheckedChanged(object sender, EventArgs e)
        {
            txtMortherGroupId.Enabled = !chkGroupEnd.Checked;
        }

        private void txtMortherGroupId_Validated(object sender, EventArgs e)
        {
            LookUp();
        }

        private void LookUp()
        {
            txtMortherGroupId.Text = LookupControl.LookupControl.Instance().LookUpControl("MaterialGroupId, MaterialGroupName, MortherGroupId",
                                                    "Mã nhóm, Tên nhóm, Nhóm mẹ", "LiMaterialGroup", "MaterialGroupId", txtMortherGroupId.Text.Trim(), "Mã nhóm", false);
        }

        private void txtMortherGroupId_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.F5)
                LookUp();
        }

        protected override void BindData()
        {
            if (base.FormAction != EditFormAction.Insert)
            {
                txtMaterialGroupId.Text = liMaterialGroup.MaterialGroupId;
                txtMaterialGroupName.Text = liMaterialGroup.MaterialGroupName;
                txtMortherGroupId.Text =  liMaterialGroup.MortherGroupId;
                chkGroupEnd.Checked = liMaterialGroup.GroupEnd;
            }
        }


        protected override bool DoCreate()
        {
            try
            {
                if (!CheckData()) return false;

                LiMaterialGroup liMaterialGroup = new LiMaterialGroup();

                liMaterialGroup.GroupEnd = chkGroupEnd.Checked;
                liMaterialGroup.MaterialGroupId = txtMaterialGroupId.Text.Trim();
                liMaterialGroup.MaterialGroupName = txtMaterialGroupName.Text.Trim();
                liMaterialGroup.MortherGroupId = chkGroupEnd.Checked ? string.Empty : txtMortherGroupId.Text.Trim();

                MaterialGroupDataAccess.Instance().CreateMaterialGroup(liMaterialGroup);
            }
            catch (Exception ex)
            {
                DisplayMessage.ShowError(ex.Message);
            }

            return true;
        }

        private bool CheckData()
        {
            if (txtMaterialGroupId.Text.Trim() == string.Empty)
            {
                erp.SetError(txtMaterialGroupId, "Chưa nhập mã nhóm");
                return false;
            }

            return true;
        }

        protected override bool DoUpdate()
        {
            try
            {
                if (!CheckData()) return false;

                LiMaterialGroup liMaterialGroup = new LiMaterialGroup();

                liMaterialGroup.GroupEnd = chkGroupEnd.Checked;
                liMaterialGroup.MaterialGroupId = txtMaterialGroupId.Text.Trim();
                liMaterialGroup.MaterialGroupName = txtMaterialGroupName.Text.Trim();
                liMaterialGroup.MortherGroupId = chkGroupEnd.Checked ? string.Empty : txtMortherGroupId.Text.Trim();

                MaterialGroupDataAccess.Instance().UpdateMaterialGroup(liMaterialGroup);
            }
            catch (Exception ex)
            {
                DisplayMessage.ShowError(ex.Message);
            }

            return true;
        }

    }
}
